<?php

/**
 * Created by PhpStorm.
 * User: zjc348
 * Date: 2020/7/20
 * Time: 19:09
 */


namespace app\common\behavior;
use think\Response;

class CronRun
{
    public function run(&$dispatch)
    {
        $origin=$_SERVER['HTTP_ORIGIN']??'';
        $arr=[
            'http://yaoxing.local.com:8080',
            'http://yaoxing.local.com:8081',
        ];
        $pass_origin='https://yaoxing.local.com';
        if(in_array($origin,$arr)){
            $pass_origin=$origin;
        }

        //        原来的,报错,给改成下面的那样了
//        $headers = [
//            header('Access-Control-Allow-Origin: '.$pass_origin),
//        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'),
//        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'),
//        header('Access-Control-Allow-Credentials:true'),
//        header('Set-Cookie:SameSite=None;Secure'),
//        ];

        $headers = [
            "Access-Control-Allow-Origin" => $pass_origin,
            "Access-Control-Allow-Credentials" => 'true',
            "Access-Control-Allow-Headers" => "Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With",
            "Access-Control-Allow-Methods" => 'GET, POST, PATCH, PUT, DELETE',
            "Set-Cookie" => 'SameSite=None;Secure'
        ];

        if ($dispatch instanceof Response) {
            $dispatch->header($headers);
        } else if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
            $dispatch['type'] = 'response';
            $response = new Response('', 200, $headers);
            $dispatch['response'] = $response;
        }

    }
}